<template>
  <vxe-modal
    v-model="visible"
    ref="mymodal"
    showFooter
    esc-closable
    title="新增快递公司"
    width="540"
    @close="closeFrom"
    @hide="closeFrom"
    remember
  >
    <div class="form-contianer">
      <el-form ref="newForm" :model="form" :rules="rules" label-width="112px">
        <el-form-item label="快递公司名称" prop="name">
          <el-input
            v-model="form.name"
            name="name"
            placeholder="快递公司名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="快递公司编码" prop="express">
          <el-input
            v-model="form.express"
            name="express"
            placeholder="快递公司编码"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFrom" size="small">取 消</el-button>
      <el-button
        type="primary"
        @click.native.prevent="submitHandle"
        :loading="loading"
        size="small"
        >新 增</el-button
      >
    </div>
  </vxe-modal>
</template>

<script>
import { add } from '@/api/priority'
export default {
  name: 'AddSupplies',
  props: {
    addDialog: {
      type: Boolean
    }
  },
  watch: {
    addDialog(val) {
      this.visible = val
    }
  },
  created() {},
  data() {
    return {
      visible: false,
      loading: false,
      form: {
        name: '',
        express: ''
      },

      rules: {
        name: [
          { required: true, message: '请输入快递公司名称', trigger: 'blur' }
        ],
        express: [
          { required: true, message: '请输入快递公司编码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeFrom() {
      this.$emit('update:addDialog', false)
    },
    submitHandle() {
      const that = this
      this.$refs.newForm.validate((valid) => {
        if (valid) {
          this.loading = true
          add(that.form)
            .then((res) => {
              this.loading = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              setTimeout(() => {
                that.closeFrom()
              }, 1000)
            })
            .catch((res) => {
              this.loading = false
            })
        } else {
          this.loading = false
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.form-contianer {
  padding: 25px 30px;
}
</style>
